package com.nf.health.app.inquiry.fragment;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.ClearEditText;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.inquiry.activity.FastInquiryActivity;
import com.nf.health.app.inquiry.activity.InquiryDetailActivity;
import com.nf.health.app.inquiry.adapter.InquiryHaveCompleteAdapter;
import com.nf.health.app.inquiry.bean.InquiryBean;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A
 * 提问fragment  已完成
 */
public class InquiryHaveCompletedFragment extends IBaseFragment implements View.OnClickListener {
    @Bind(R.id.tw_consul_listview)
    DragListView twConsulListview;
    @Bind(R.id.et_search)
    ClearEditText etSearch;

    private static final String GET_INQUIRY_LIST = "get_inquiry_list";
    private static final String DELETE_INQUIRY_BY_ID = "DELETE_INQUIRY_BY_ID";
    @Bind(R.id.tv_go_inquiry)
    TextView tvGoInquiry;
    @Bind(R.id.ll_no_inquiry)
    LinearLayout llNoInquiry;

    private List<InquiryBean> mListBean = new ArrayList<InquiryBean>();

    private int page = 1;
    private int pageSize = 20;

    private InquiryHaveCompleteAdapter mAdapter;
    private boolean isLoadMore = false;//记录加载更多
    private String keyWords = "";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_tiwen);
        ButterKnife.bind(this, rootView);
        twConsulListview.setRefreshableAndLoadMoreable(true, true);
        initListener();
        initData(GET_INQUIRY_LIST);
    }

    private void initListener() {
        tvGoInquiry.setOnClickListener(this);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etSearch.isHasFoucs()) {
                    etSearch.setClearIconVisible(s.length() > 0);
                }
                keyWords = s.toString();
                //每次搜索内容改变,清空集合
                page = 1;
                mListBean.clear();
                initData(GET_INQUIRY_LIST);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        twConsulListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                page = 1;
                initData(GET_INQUIRY_LIST);
            }

            @Override
            public void onLoadMore() {
                page++;
                initData(GET_INQUIRY_LIST);
            }
        });
        twConsulListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString(InquiryDetailActivity.INQUIRY_ID, mListBean.get(position - 1).getInquiryid());
                bundle.putBoolean(InquiryDetailActivity.IS_COMPLET, true);
                ActivityUtils.showActivity(getActivity(), InquiryDetailActivity.class, bundle);
            }
        });
    }

    private void initData(String flag) {
        //1代表完成
        provider.getInquiryList(page + "", pageSize + "", "1", keyWords, flag);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        twConsulListview.completeLoadMore();
        twConsulListview.completeRefresh();
        if (GET_INQUIRY_LIST.equals(actionName)) {
            List<InquiryBean> newListBean = (List<InquiryBean>) object;
            if (newListBean.size() == 0) {
                showToast("没有更多数据");
                //一条数据都没有
                if (mListBean.size()==0){
                    twConsulListview.setVisibility(View.GONE);
                    llNoInquiry.setVisibility(View.VISIBLE);
                }
                return;
            }
            if (!isLoadMore) {
                mListBean.clear();
            }
            mListBean.addAll(newListBean);
            twConsulListview.setVisibility(View.VISIBLE);
            llNoInquiry.setVisibility(View.GONE);
            if (mAdapter == null) {
                mAdapter = new InquiryHaveCompleteAdapter(getActivity(), mListBean);
                twConsulListview.setAdapter(mAdapter);
                setAdapterLintener();
            }
            mAdapter.notifyDataSetChanged();
        } else if (DELETE_INQUIRY_BY_ID.equals(actionName)) {
            showToast("删除问诊成功");
        }
    }

    /**
     * 处理适配器item中点击事件,删除和点击
     */
    private void setAdapterLintener() {
        mAdapter.setOnMyClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String flag) {
                if ("delete".equals(flag)) {
                    provider.deleteInquiryById(mListBean.get(position).getInquiryid(), DELETE_INQUIRY_BY_ID);

                    //请求删除后直接更新(若删除不成功会有问题)
                    mListBean.remove(position);
                    //一条数据都没有
                    if (mListBean.size()==0){
                        llNoInquiry.setVisibility(View.VISIBLE);
                    }
                    mAdapter.notifyDataSetChanged();
                } else if ("click".equals(flag)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(InquiryDetailActivity.INQUIRY_ID, mListBean.get(position).getInquiryid());
                    bundle.putBoolean(InquiryDetailActivity.IS_COMPLET, true);
                    ActivityUtils.showActivity(getActivity(), InquiryDetailActivity.class, bundle);
                }
            }
        });
    }

    public void onResume() {
        super.onResume();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_go_inquiry){
            ActivityUtils.skipActivity(getActivity(), FastInquiryActivity.class);
        }
    }
}